package photorganizer.presentation.common.action;

import java.awt.event.ActionEvent;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;

import photorganizer.common.resource.ResourceHome;

public class SimpleAction extends AbstractAction {

	public static final String ACCELERATOR = "accelerator";

	public static final String LARGE_ICON = "largeIcon";

	public static final String LONG_DESCRIPTION = "longDescription";

	public static final String MNEMONIC = "mnemonic";

	public static final String MNEMONIC_INDEX = "mnemonicIndex";

	public static final String NAME = "name";

	public static final String SELECTED = "selected";

	public static final String SHORT_DESCRIPTION = "shortDescription";

	public static final String SMALL_ICON = "smallIcon";

	private static final long serialVersionUID = 6560875119069164225L;

	public SimpleAction(String command) {
		this(command, true);
	}

	public SimpleAction(String command, boolean enabled) {
		setActionCommand(command);
		setEnabled(enabled);
		loadProperties();
	}

	public KeyStroke getAccelerator() {
		return (KeyStroke) getValue(Action.ACCELERATOR_KEY);
	}

	public void setAccelerator(KeyStroke accelerator) {
		putValue(Action.ACCELERATOR_KEY, accelerator);
	}

	public String getActionCommand() {
		return (String) getValue(Action.ACTION_COMMAND_KEY);
	}

	public void setActionCommand(String command) {
		putValue(Action.ACTION_COMMAND_KEY, command);
	}

	public Icon getLargeIcon() {
		return (Icon) getValue(Action.LARGE_ICON_KEY);
	}

	public void setLargeIcon(Icon icon) {
		putValue(Action.LARGE_ICON_KEY, icon);
	}

	public String getLongDescription() {
		return (String) getValue(Action.LONG_DESCRIPTION);
	}

	public void setLongDescription(String description) {
		putValue(Action.LONG_DESCRIPTION, description);
	}

	public Integer getMnemonic() {
		return (Integer) getValue(Action.MNEMONIC_KEY);
	}

	public void setMnemonic(Integer mnemonic) {
		putValue(Action.MNEMONIC_KEY, mnemonic);
	}

	public Integer getMnemonicIndex() {
		return (Integer) getValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY);
	}

	public void setMnemonicIndex(Integer mnemonicIndex) {
		putValue(Action.DISPLAYED_MNEMONIC_INDEX_KEY, mnemonicIndex);
	}

	public String getName() {
		return (String) getValue(Action.NAME);
	}

	public void setName(String name) {
		putValue(Action.NAME, name);
	}

	public Boolean isSelected() {
		return (Boolean) getValue(Action.SELECTED_KEY);
	}

	public void setSelected(Boolean selected) {
		putValue(Action.SELECTED_KEY, selected);
	}

	public String getShortDescription() {
		return (String) getValue(Action.SHORT_DESCRIPTION);
	}

	public void setShortDescription(String description) {
		putValue(Action.SHORT_DESCRIPTION, description);
	}

	public Icon getSmallIcon() {
		return (Icon) getValue(Action.SMALL_ICON);
	}

	public void setSmallIcon(Icon icon) {
		putValue(Action.SMALL_ICON, icon);
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@SuppressWarnings("unused")
	public void actionPerformed(ActionEvent event) {
	}

	private void setAccelerator(String accelerator) {
		setAccelerator(KeyStroke.getKeyStroke(accelerator));
	}

	private void setLargeIcon(String name) {
		URL url = ClassLoader.getSystemResource(name);
		setLargeIcon(new ImageIcon(url));
	}

	private void setMnemonic(String mnemonic) {
		setMnemonic(new Integer(mnemonic.charAt(0)));
	}

	private void setMnemonicIndex(String mnemonicIndex) {
		setMnemonicIndex(new Integer(mnemonicIndex));
	}

	private void setSelected(String selected) {
		setSelected(new Boolean(selected));
	}

	private void setSmallIcon(String name) {
		URL url = ClassLoader.getSystemResource(name);
		setSmallIcon(new ImageIcon(url));
	}

	private void loadProperties() {
		try {
			setAccelerator(ResourceHome.get(this, ACCELERATOR));
		}
		catch (Exception ignored) {
		}

		try {
			setLargeIcon(ResourceHome.get(this, LARGE_ICON));
		}
		catch (Exception ignored) {
		}

		try {
			setLongDescription(ResourceHome.get(this, LONG_DESCRIPTION));
		}
		catch (Exception ignored) {
		}

		try {
			setMnemonic(ResourceHome.get(this, MNEMONIC));
		}
		catch (Exception ignored) {
		}

		try {
			setMnemonicIndex(ResourceHome.get(this, MNEMONIC_INDEX));
		}
		catch (Exception ignored) {
		}

		try {
			setName(ResourceHome.get(this, NAME));
		}
		catch (Exception ignored) {
		}

		try {
			setSelected(ResourceHome.get(this, SELECTED));
		}
		catch (Exception ignored) {
		}

		try {
			setShortDescription(ResourceHome.get(this, SHORT_DESCRIPTION));
		}
		catch (Exception ignored) {
		}

		try {
			setSmallIcon(ResourceHome.get(this, SMALL_ICON));
		}
		catch (Exception ignored) {
		}
	}

}
